package com.example.hotel_manage.service.employee;

import com.example.common.constant.ErrorCode;
import com.example.common.exception.BusinessException;
import com.example.common.pojo.vo.PageVO;
import com.example.hotel_manage.dao.employee.EmployeeDAO;

import com.example.hotel_manage.dao.position.PositionDAO;
import com.example.hotel_manage.pojo.dto.employee.EmployeeAddDTO;
import com.example.hotel_manage.pojo.dto.employee.EmployeeUpdateDTO;
import com.example.hotel_manage.pojo.mapper.employee.EmployeeMapper;
import com.example.hotel_manage.pojo.po.employee.EmployeePO;

import com.example.hotel_manage.pojo.po.position.PositionPO;
import com.example.hotel_manage.pojo.qo.employee.EmployeeQO;
import com.example.hotel_manage.pojo.vo.employee.EmployeeListVO;
import com.example.hotel_manage.pojo.vo.employee.EmployeeShowVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * 【员工】增删改查
 */
@Service
public class EmployeeService {

    @Autowired
    private EmployeeDAO employeeDAO;
    @Autowired
    private PositionDAO positionDAO;


    /**
     * 新增【员工】
     *
     * @param employeeDTO
     * @return
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public EmployeePO save(EmployeeAddDTO employeeDTO) {
        EmployeePO employee = EmployeeMapper.INSTANCE.fromAddDTO(employeeDTO);
        if (employee.getP_id() != null) {
            Assert.isTrue(positionDAO.exist(employee.getP_id()), "职位ID有误");
        }
        employeeDAO.save(employee);
        return employee;
    }

    /**
     * 修改【员工】
     *
     * @param employeeUpdateDTO
     * @return
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public EmployeePO update(EmployeeUpdateDTO employeeUpdateDTO) {
        Integer id = employeeUpdateDTO.getId();
        EmployeePO employee = this.getEmployee(id, true);
        EmployeeMapper.INSTANCE.setUpdateDTO(employee, employeeUpdateDTO);
        if (employee.getP_id() != null) {
            Assert.isTrue(positionDAO.exist(employee.getP_id()), "职位ID有误");
        }
        employeeDAO.update(employee);
        return employee;
    }

    /**
     * 查询分页列表
     *
     * @param employeeQO
     * @return
     */
    public PageVO<EmployeeListVO> list(EmployeeQO employeeQO) {
        PageVO<EmployeeListVO> page = employeeDAO.findByPage(employeeQO);
        return page;
    }

    /**
     * 根据主键获取【员工】
     *
     * @param id    主键
     * @param force 是否强制获取
     * @return
     */
    public EmployeePO getEmployee(Integer id, boolean force) {
        EmployeePO employee = employeeDAO.findById(id);
        if (force && employee == null) {
            throw new BusinessException(ErrorCode.RECORD_NOT_FIND);
        }
        return employee;
    }


    /**
     * 查询【员工】详情
     *
     * @param id
     * @return
     */
    public EmployeeShowVO show(Integer id) {
        EmployeePO employee = this.getEmployee(id, true);
        EmployeeShowVO showVO = EmployeeMapper.INSTANCE.toShowVO(employee);
        if (employee.getP_id() != null) {
            PositionPO _positionPO = positionDAO.findById(employee.getP_id());
            showVO.setP_name(_positionPO.getName());
        }
        return showVO;
    }

    /**
     * 删除【员工】
     *
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public int delete(Integer... ids) {
        int count = 0;
        for (Integer id : ids) {
            count += employeeDAO.delete(id);
        }
        return count;
    }


}


